/*
NoBuffer Notepad Copyright (C) 2015  Mahmoud Jaoune

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "textedit.h"
#include "filethread.h"

NBN_TextEdit::NBN_TextEdit(QWidget *parent)
{
    setObjectName("textEdit");
    setParent(parent);

    reloadSettings();
}

void NBN_TextEdit::NBN_LoadFile(const char *fileName)
{
    NBN_FileThread *fileThread = new NBN_FileThread(0, QString(fileName), QString(fileName), this);

    //Too many connects since we got multithreading
    connect(fileThread, SIGNAL(NBN_BufferReady(QString)), this, SLOT(NBN_LoadBuffer(QString)));
    connect(fileThread, SIGNAL(NBN_FileLoadProcess(unsigned int, unsigned int)), this->parent()->parent()->parent(), SLOT(NBN_setProgress(unsigned int, unsigned int)));
    connect(fileThread, SIGNAL(NBN_FileLoadFinished()), this->parent()->parent()->parent(), SLOT(NBN_endProgressGUI()));
    connect(fileThread, SIGNAL(NBN_FileLoadFinished()), this, SLOT(NBN_ResetCursor()));
    connect(fileThread, SIGNAL(NBN_NumberOfBlocks(int, QString)), this->parent()->parent()->parent(), SLOT(NBN_SetTotalBlocks(int, QString)));
    connect(this->parent()->parent()->parent(), SIGNAL(NBN_OnSelectBlock(unsigned int, NBN_TextEdit*)), this, SLOT(NBN_LoadText(unsigned int, NBN_TextEdit*)));
    connect(this, SIGNAL(NBN_EnvironmentReady(uint)), fileThread, SLOT(NBN_LoadBlock(uint)));
    connect(fileThread, SIGNAL(NBN_SendClearText()), this, SLOT(NBN_ClearText()));
    connect(this->parent()->parent()->parent(), SIGNAL(NBN_CleanThread(QString)), fileThread, SLOT(NBN_CloseThread(QString)));

    connect(this->parent()->parent()->parent(), SIGNAL(NBN_SendSave(QString, BlockDB)), fileThread, SLOT(NBN_SaveFile(QString, BlockDB)));
    connect(this->parent()->parent()->parent(), SIGNAL(NBN_SendSaveAs(QString, QString, BlockDB)), fileThread, SLOT(NBN_SaveAsFile(QString, QString, BlockDB)));
    connect(this->parent()->parent()->parent(), SIGNAL(NBN_SendUndo(QString)), fileThread, SLOT(on_UndoAction(QString)));
    connect(this->parent()->parent()->parent(), SIGNAL(NBN_SendRedo(QString)), fileThread, SLOT(on_RedoAction(QString)));
    connect(this->parent()->parent()->parent(), SIGNAL(NBN_SendCut(QString)), fileThread, SLOT(on_CutAction(QString)));
    connect(this->parent()->parent()->parent(), SIGNAL(NBN_SendCopy(QString)), fileThread, SLOT(on_CopyAction(QString)));
    connect(this->parent()->parent()->parent(), SIGNAL(NBN_SendPaste(QString)), fileThread, SLOT(on_PasteAction(QString)));

    connect(this->parent()->parent()->parent(), SIGNAL(reloadSettings()), this, SLOT(reloadSettings()));

    connect(fileThread, SIGNAL(destroyed()), this, SLOT(deleteLater()));

    fileThread->start(QThread::InheritPriority);

}

/*
 * Inserts texts into textEdit in stages
 */
void NBN_TextEdit::NBN_LoadBuffer(QString buffer)
{
    this->appendPlainText(buffer);
}

/*
 * Clear text edit to load a new block in it
 */
void NBN_TextEdit::NBN_ClearText()
{
    this->clear();
}

/*
 * Loads text into targetted TextEdit after the TextEdit gets cleared
 */
void NBN_TextEdit::NBN_LoadText(unsigned int index, NBN_TextEdit *currentTextEdit)
{
    if(currentTextEdit == this)
    {
        emit NBN_EnvironmentReady(index);
    }
}

/*
 * Moves cursor to beginning after file has finished loading
 */
void NBN_TextEdit::NBN_ResetCursor()
{
    this->moveCursor(QTextCursor::Start);
    this->ensureCursorVisible();
}

/*
 * Reloads settings of all TextEdits when the user changes any of them
 */
void NBN_TextEdit::reloadSettings()
{
    QSettings settings("settings.ini", QSettings::IniFormat);
    settings.beginGroup("Options");

    //Font
    QFont font;
    font.fromString(settings.value("font").toString());
    this->setFont(font);

    //Font color
    QColor fontColor;
    fontColor.setNamedColor(settings.value("fontColor").toString());

    if(fontColor.isValid())
    {
        QString colorQuery(this->styleSheet());
        colorQuery.push_back("color: ");
        colorQuery.push_back(fontColor.name());
        colorQuery.push_back(";");

        this->setStyleSheet(colorQuery);
    }

    //Background color
    QColor bgColor;
    bgColor.setNamedColor(settings.value("bgColor").toString());

    if(bgColor.isValid())
    {
        QString bgColorQuery(this->styleSheet());
        bgColorQuery.push_back("background-color: ");
        bgColorQuery.push_back(bgColor.name());
        bgColorQuery.push_back(";");

        this->setStyleSheet(bgColorQuery);
    }


    settings.endGroup();
}

/* End of MenuBar actions */

